/*
 * Created on 30-ene-2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.juanfrivaldes.cio2005.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.dao.DataAccessException;
import org.springframework.mail.MailException;

import com.juanfrivaldes.cio2005.domain.Ponencia;
import com.juanfrivaldes.cio2005.domain.Usuario;

/**
 * @author root
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class InscribirseAction extends AutentificacionUsuarioAction {

	public InscribirseAction() {}
	
	private static Log log = LogFactory.getLog(InscribirseAction.class);

	protected ActionForward protectedExecute(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		InscribirseForm insForm=(InscribirseForm)form;

		try {

			//esta cutreria es para poder utilizar el metodo del padre
			String userMail=insForm.getEmail();
			
			ActionForward aForward = this.performAutentification(mapping,
					request, insForm.getEmail(), insForm.getPassword());

			
			// limpiamos el formulario
			insForm.setPassword("");
			insForm.setEmail("");
			
			
			
			if (aForward!=null){
				return aForward;
			}
			else{
				this.getCio2005().inscribeUser(userMail);
				try{
					this.getCio2005().enviaMailInscripcion(userMail);
				}catch(MailException exc){
					log.trace("Excepcion al enviar mail de confirmacion de inscripcion: "+exc.getMessage());
				}
				log.trace("Inscrito en el congreso el usuario "+userMail);
			}

		} catch (Exception e) {
			log.trace("fallo en la base de datos al inscribirse: "+e.getMessage());
			return mapping.findForward("failure");
		}

		return mapping.findForward("success");

	}

	

}
